﻿import kliment.display.MCC;
/**
 * ...
 * @author	Kliment
 * @version	1.1.0
 */
class kliment.utils.FPS {
	static private var _instance:FPS;
	static private var _fps:Number = 30;
	static private var _lasttime:Number;
	private var _timepassed:Number;
	private var _target_mc:MovieClip;
	
	public function FPS() {
		if (!_instance) {
			_target_mc = MCC.create(_root);
			_target_mc.onEnterFrame = _calcFPS;
		}
	}
	
	static public function init():Void {
		if (!_instance) {
			_instance = new FPS();
			var time:Date = new Date();
			_lasttime = time.getMilliseconds();
		}
	}
	
	static public function getFPS():Number {
		init();
		return _fps;
	}
	
	private function _calcFPS():Void {
		var time:Date = new Date();
		var mSeconds:Number = time.getMilliseconds();
		var dTime:Number = mSeconds - _lasttime;
		_timepassed = (dTime >= 0)? dTime: (1000 + dTime);
		_fps = Math.round(1000 / _timepassed);
		_lasttime = mSeconds;
	}
}